jQuery(function($) {
    $.extend({
        serializeJSON: function(obj) {
            var t = typeof(obj);
            if(t != "object" || obj === null) {
                // simple data type
                if(t == "string") obj = '"' + obj + '"';
                return String(obj);
            } else {
                // array or object
                var json = [], arr = (obj && obj.constructor == Array);
 
                $.each(obj, function(k, v) {
                    t = typeof(v);
                    if(t == "string") v = '"' + v + '"';
                    else if (t == "object" & v !== null) v = $.serializeJSON(v)
                    json.push((arr ? "" : '"' + k + '":') + String(v));
                });
 
                return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
            }
        }
    });
});

var search_data ="";
var page_no =1;

function Inint_AJAX() 
{
	try 
	{ 
		return new ActiveXObject("Msxml2.XMLHTTP");  
	} 
	catch(e) 
	{} //IE
	try 
	{ 
		return new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	catch(e) {} //IE
	try 
	{ 
		return new XMLHttpRequest(); 
	} catch(e) {} //Native Javascript
	alert("XMLHttpRequest not supported");
	return null;
};
$(document).ready(function() {
//						   $("#loader").hide();
//alert('sfsf');							 	
$("#edit_user").hide();
$("#edit_message").hide();
$("#main_content").load("call_ajax_manage_user.php?search=&&page=1",function(){
							$("#loader").hide();
							});


});	

function search_user(value,page){
	search_data = value;
	page_no= page;
$("#main_content").html('');
close_edit();

$("#loader").show();
	$("#main_content").load("call_ajax_manage_user.php?search=" + value+ "&&page=" + page ,function(){
							$("#loader").hide();
							});

}

function status_handler(value,user_id){
$("#user_status_" + user_id).html('Loading....');	
$("#user_status_" + user_id).load("call_ajax_manage_user.php?status="+ value+"&&user_id=" + user_id,function(){
							
							});	
}

function close_edit(){
$("#edit_user").hide();
$(".user_row").show();
}

function edit_user(user_id) {
	
$(".user_row:not('#user_row_"+ user_id +"')").slideUp(50);
$("#edit_message").show();



$.getJSON("call_ajax_manage_user.php?change_user=" + user_id,
   function(data) {
	  
	$("#user_id").val(data.user_id);   
   $("#name").val(data.name);
   $("#usertype").val(data.user_type);
   check_usertype(data.user_type);
   
 			if(data.user_type == 1){
				$("#zone").val(data.zone_id);
				
			}
			else if(data.user_type == 5) {
			$("#zone").val(data.zone_id);
			load_circle(data.zone_id,data.circle_id);
			load_division(data.circle_id,data.div_id);
			load_sub_div(data.div_id,data.sub_div_id);
			
		
			
			}
			
			$("#designation").val(data.designation);
			$("#login_id").val(data.login_id);
			$("#emailid").val(data.email);
			 $("#edit_message").hide();
			 $("#edit_user").show();

    });





	
}


function createuser(event)
{
   if(document.getElementById('name').value=="")	
	{
		alert('Please Enter Name Field');
	    document.getElementById('name').focus();
		return false;
	}
    
	if(document.admin.usertype[document.admin.usertype.selectedIndex].value==0)
	{
	  alert("Please Select Usertype Field");
	  document.admin.usertype.focus();
	   return false;
	}
	else if(document.admin.usertype[document.admin.usertype.selectedIndex].value==2){
		if($("#zone").val()==0){
		alert('Please Select Zone Field');
		$("#zone").focus();
		return false;
		}
	}
	else if(document.admin.usertype[document.admin.usertype.selectedIndex].value==3){
		if($("#zone").val()==0){
		alert('Please Select Zone Field');
		$("#zone").focus();
		return false;
		}
		if($("#circle").val()==0){
		alert('Please Select Circle Field');
		$("#circle").focus();
		return false;
		}
	}
	else if(document.admin.usertype[document.admin.usertype.selectedIndex].value==4){
		if($("#zone").val()==0){
		alert('Please Select Zone Field');
		$("#zone").focus();
		return false;
		}
		if($("#circle").val()==0){
		alert('Please Select Circle Field');
		$("#circle").focus();
		return false;
		}
		if($("#division").val()==0){
		alert('Please Select Division Field');
		$("#division").focus();
		return false;
		}
	}
	else if(document.admin.usertype[document.admin.usertype.selectedIndex].value==5){
		if($("#zone").val()==0){
		alert('Please Select Zone Field');
		$("#zone").focus();
		return false;
		}
		if($("#circle").val()==0){
		alert('Please Select Circle Field');
		$("#circle").focus();
		return false;
		}
		if($("#division").val()==0){
		alert('Please Select Division Field');
		$("#division").focus();
		return false;
		}
		if($("#sub_division").val()==0){
		alert('Please Select Sub Division Field');
		$("#sub_division").focus();
		return false;
		}

	}
	
	  
	if(document.admin.designation[document.admin.designation.selectedIndex].value==0)
	{
	  alert("Please Select Designation Field");
	  document.admin.designation.focus();
	   return false;
	}
	
	if(document.getElementById('login_id').value=="")	
	{
		alert('Please Enter Loginid Field');
	    document.getElementById('login_id').focus();
		return false;
	}
	
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.getElementById('emailid').value;
   if(reg.test(address) == false)
   {
      alert('Invalid Email Address');
	  document.getElementById('emailid').focus();
      return false;
   }
   
   else{
	var http = Inint_AJAX() ;
	var obj = {
		"user_id": $("#user_id").val(),
		"name": $("#name").val(),
		"usertype": $("#usertype").val(),
		"zone": $("#zone").val(),
		"circle": $("#circle").val(),
		"division":$("#division").val(),
		"sub_division":$("#sub_division").val(),
		"designation":$("#designation").val()
		
		};
		
	var j = $.serializeJSON(obj);
	

	
	$("#user_row_"+ obj.user_id).html("<td colspan='2' align='center'>Saving...</td>");
	http.onreadystatechange = function(){
	if(http.readyState ==4 && http.status == 200)
	alert(http.responseText);
	}
	http.open("post","call_ajax_manage_user.php?content="+j,true);
	http.send(null);
	
	search_user(search_data,page_no);
   return false;
   }
	

}

function check_usertype(value){
	
	if(value==2) {
		$(".user_type").hide();
		$("#zone").show();
	}
	if(value==3) {
		$(".user_type").hide();
		$("#zone").show();
		$("#circle").show();
	}
		if(value==4) {
		$(".user_type").hide();
		$("#zone").show();
		$("#circle").show();
		$("#division").show();
	}
	
		if(value==5) {
		$(".user_type").hide();
		$("#zone").show();
		$("#circle").show();
		$("#division").show();
		$("#sub_division").show();
	}
	if(value < 2) {
	$(".user_type").hide();
	}
	
}

function load_circle(value,index){
		
	
	var http =  Inint_AJAX();
	
	http.onreadystatechange = function(){
	$("#circle").html(http.responseText);
	$("#circle").val(index).attr("selected");
	}
	http.open("post","call_ajax_create_user.php?load_circle="+value+"&&index="+index,true);
	http.send(null);
	
	
}

function load_division(value,index){
		
	
	var http =  Inint_AJAX();
	
	http.onreadystatechange = function(){

	$("#division").html(http.responseText);
	$("#division").val(index).attr("selected");
	}
	http.open("post","call_ajax_create_user.php?load_division="+value+"&&index="+index,true);
	http.send(null);
	
	
}

function load_sub_div(value,index){
		
	
	var http =  Inint_AJAX();
	
	http.onreadystatechange = function(){

	$("#sub_division").html(http.responseText);
	$("#sub_division").val(index).attr("selected");
	}
	http.open("post","call_ajax_create_user.php?load_sub_div="+value+"&&index="+index,true);
	http.send(null);
	
	
}



